<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<script type="text/html" id="barDemo">
    <!--    <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>-->
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
<fieldset class="layui-elem-field layui-field-title">
    <legend>货物验收</legend>
</fieldset>



<table class="layui-hide" id="test" lay-filter="test"></table>
<!--<div class="layui-form-item">-->
<!--    <div class="layui-col-xs12">-->
<!--        <div class="layui-col-xs2 layui-col-xs-offset4">-->
<!--            <button data-method type="button" class="layui-btn" onclick="inspect()">检验完毕</button>-->
<!--        </div>-->
<!--        <div class="layui-col-xs2">-->
<!--            <button type="button" class="layui-btn" onclick="cancel1()">取消</button>-->
<!--        </div>-->
<!--    </div>-->
<!--</div>-->


<table>

</table>
<script src="lib/jquery-1.12.4.js"></script>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->


<script>

    function test(data){

    }

    layui.use('table', function(){
        var table = layui.table;

        var deliveryOrderId = parent.deliveryOrderId;
        // alert(deliveryOrderId);
        console.log(deliveryOrderId);
        //监听行工具事件
        table.render({
            elem: '#test'
            ,url:'pk/deliveryOrder/listDeliveryOrderDById'
            ,where:{
                deliveryOrderId :deliveryOrderId
            }
            ,parseData: function(res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.obj.count, //解析数据长度
                    "data": res.obj.list //解析数据列表
                };
            }
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,cols: [[
                {field:'goodsName', title: '商品名称', sort: true}
                ,{field:'quantityOfGoods', title: '应收数量', sort: true}
                ,{field:'quantityReceived', title: '实收数量', sort: true}
                ,{field:'quantityOfDefectiveGoods', title: '残次品数量', sort: true}
                // ,{field:'type', title: '类型', sort: true}
                // ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                // layer.confirm('真的删除行么', function(index){
                //     obj.del();
                //     layer.close(index);
                // });
            } else if(obj.event === 'edit'){
                var layer = layui.layer;
                layer.open({
                    type: 2,
                    area: ['900px','510px'],
                    content: 'updateGoods.html' //这里content是一个普通的String
                });
            }
        });
    });
    function insertDelivery() {
        var layer = layui.layer;
        layer.open({
            type: 2,
            area: ['900px','510px'],
            content: 'insertGoods.html' //这里content是一个普通的String
        });
    }
    function inspect() {
        var layer = layui.layer;
        layer.confirm('确认所有验收数据无误？', { btn: ['确认','取消'],btn1: function(){
                var table = layui.table;
                console.log(table.cache.test);
                // var data = JSON.stringify(table.cache.test);
                // console.log(data.length); 显示字符长度,不是数组长度
                var data1 = [];
                for(var i=0;i<table.cache.test.length;i++){
                    if(table.cache.test[i].quantityReceived===undefined){
                        alert("是undefined")
                    }
                }
                for(var i=0;i<table.cache.test.length;i++){
                    data1.push({"id":table.cache.test[i].id,"quantityReceived":parseInt(table.cache.test[i].quantityReceived),"quantityOfDefectiveGoods":parseInt(table.cache.test[i].quantityOfDefectiveGoods),"deliveryOrderId":parseInt(table.cache.test[i].deliveryOrderId)});
                }
                console.log(data1);
                var data = JSON.stringify(data1);

                $.ajax({
                    url: 'pk/deliveryOrder/updateDeliveryOrderDInspect',
                    type: "POST",
                    data: data,
                    contentType:"application/json;charset=UTF-8",
                    success:function () {
                        layer.msg("验收完成");
                        parent.layer.closeAll();
                        parent.selectDO();
                    }
                });
            },
            btn2: function(){

            }
        });
        // layer.confirm('请确认所有验收数据无误', function(index) {
        //
        //     layer.close(index);
        // });
    }
</script>
</body>
</html>